﻿using System;
using System.Collections.Generic;
using System.Text;
using ZLDC.Common.Estate;
using ZLDC.Topo.BLL.Estate;
using ZLDC.Topo.Model.Estate;

namespace ZLDC.Estate.TM
{
    public partial class CdepartmentAdd : _PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadArea();
            }
        }

        // 加载片区
        private void LoadArea()
        {
            int deptid = _Request.GetInt("deptid", 0);

            if (deptid <= 0)
            {
                divArea.InnerHtml = "请先选择您要操作的部门！";
                return;
            }

            AuthorizationInfo info = AuthorizationBLL.p_authorization_detail(deptid);
            string areas = info.Areas;          // 已授权的片区
            string estates = info.Estates;      // 已授权的楼盘

            int parentid = 1;       // 深圳
            StringBuilder sb = new StringBuilder();

            sb.Append("<table style=\"width: 98%; background:#000000; \" cellpadding=\"0\" cellspacing=\"1\" border=\"0\" >");
            sb.Append("<tr style=\"background-color:#F1FADE; text-align:center; font-weight:bold;\"><td style=\"width:10%;\">城区</td>");
            sb.Append("<td>片区</td>");
            sb.Append("</tr>");

            List<AreaInfo> list = ZLDC.Topo.BLL.Estate.AreaBLL.AreaList2(parentid);
            
            string areaList = "";

            if (list != null && list.Count > 0)
            {
                foreach (AreaInfo item in list)
                {
                    List<AreaInfo> list2 = ZLDC.Topo.BLL.Estate.AreaBLL.AreaList2(item.Id);

                    sb.Append("<tr>");
                    sb.Append("<td style=\"background:#FFFFFF;\">" + item.Name + "</td>");
                    sb.Append("<td style=\"background:#FFFFFF;\">");
                    sb.Append("<table style=\"width: 100%; \"><tr>");
                    for (int i = 0; i < list2.Count; i++)
                    {
                        AreaInfo item2 = list2[i];

                        if (i > 0 && i % 8 == 0)
                        {
                            sb.Append("</tr><tr>");
                        }

                        sb.Append("<td>");
                        // 判断当前部门是否已存在该片区授权                            
                        if (_Tools.IsExistsStr(areas, item2.Id.ToString()))
                        {
                            areaList += item2.Id + "|";
                            //sb.Append("<input id=\"cbArea_" + item2.Id + "\" type=\"checkbox\" checked onclick=\"return addArea(this, " + item2.Id + ")\" />");
                            sb.Append("<input type='checkbox' name='checkbox_" + item2.Id + "' checked id='" + item2.Id + "' onclick=\"cbSelect(this, " + item2.Id + ")\" />");
                            sb.Append("<font color='red'>" + item2.Name + "</font>");
                        }
                        else
                        {
                            //sb.Append("<input id=\"cbArea_" + item2.Id + "\" type=\"checkbox\" onclick=\"return addArea(this, " + item2.Id + ")\" />");
                            sb.Append("<input type='checkbox' name='checkbox_" + item2.Id + "' id='" + item2.Id + "' onclick=\"cbSelect(this, " + item2.Id + ")\" />");
                            sb.Append(item2.Name);
                        }
                        
                        sb.Append("</td>");
                    }
                    sb.Append("</tr></table>");
                    sb.Append("</td>");
                    sb.Append("</tr>");
                }
            }
            sb.Append("</table>");
            sb.Append("<input type='hidden' value='"+areaList+"' id='hID' />");

            divArea.InnerHtml = sb.ToString();
        }
    }
   
}
